<template>
  <div class="login-container">
    <!-- <meta http-equiv="refresh" content="30"> -->
    <!-- <video src="" width="100%" height="100%" /> -->
    <div class="login-video">
      <video src="@/assets/common/video.3cf57b17.mp4" autoplay="" loop="" muted="" style="width: 100%; height: 100%; object-fit: fill;" />
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/logo1.33ef7fa0.png" alt="">
        </h3>
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model.trim="loginForm.account"
          placeholder="请输入账户"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model.trim="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <i class="el-icon-success" />
        </span>
        <el-input
          v-model.trim="loginForm.code"
          placeholder="请输入验证码"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <div>
          <img :src="url" alt="codeImage" class="code-image" @click="changeImg">
        </div>
      </el-form-item>

      <el-button :loading="loading" type="primary" class="login-button" @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">
          仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！
        </span>
      </div>

    </el-form>
    <FooterName class="login-footer" />
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import FooterName from '@/components/Footer/FooterName.vue'
import { getVerification } from '@/api/user'

export default {
  name: 'Login',
  components: { FooterName },
  data() {
    return {
      url: '',
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        key: ''
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      loginRules: {
        account: [
          { required: true, message: '请输入正确的账户', trigger: ['blur', 'change'] },
          { min: 6, max: 16, message: '手机号不合法', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: ['blur', 'change'] },
          { min: 6, max: 16, message: '长度在 6 到 16个字符', trigger: ['blur', 'change'] }
        ],
        verification: [
          { required: true, message: '请输入正确的验证码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  // 页面加载前获取验证码
  created() {
    this.loginForm.key = this.randomNum()
    // console.log(this.loginForm.key)
    this.getCode()
  },
  methods: {
    // 获取验证码
    async getCode() {
      const result = await getVerification(this.loginForm.key)
      this.url = URL.createObjectURL(result.data)
    },
    // 点击图片刷新验证码
    changeImg() {
      this.getCode()
    },
    // 密码显示隐藏
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 兜底校验
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        valid && this.doLogin()
      })
    },
    // 发请求登录
    async doLogin() {
      // console.log('要登录')
      await this.$store.dispatch('user/login', this.loginForm)
      this.$message.success('登录成功')
      this.$router.push('/')
    },
    randomNum() {
      let strs = ''
      const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
      ]
      for (let i = 0; i < 16; i++) {
        const id = parseInt(Math.random() * 61)
        strs += chars[id]
      }
      return strs
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

// $bg:#283443;
// $light_gray:#fff;
// $cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  // .login-video{
  //   position: fixed;
  // }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      // color: $light_gray;
      height: 47px;
      // caret-color: $cursor;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: #e8f0fe;
    border: 1px solid #bac0cd;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#2d3a4b;
// $dark_gray:#889aa4;
// $light_gray:#eee;

.login-container {
  height: 100%;
  width: 100%;
  // background-color: pink;
  overflow: hidden;

  .login-form {
    position: fixed;
    top: 45%;
    left: 45%;
    margin: -200px 0 0 -160px;
    width: 450px;
    height: 490px;
    padding: 36px 40px;
    background: #fff;
    border-radius: 10px;
    z-index: 2;
  }

  .tips {
    font-size: 14px;
    color: red;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    // color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      // color: $light_gray;
      // margin: 0px auto 40px auto;
      width: 100%;
      height: 54px;
      text-align: center;
      font-weight: bold;
      img{
        display:inline-block;
        width: 60%;
        height: 100%;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    // color: $dark_gray;
    cursor: pointer;
    // user-select: none;
  }

  .login-button {
    width: 100%;
    margin: 20px 0 10px 0;
  }
// .login-footer {
//     position: absolute;
//     bottom: 30px;
//     width: 100%;
//     font-size: 14px;
//     color:#fff;
//     text-align: center;
//     span {
//       margin-left: 20px;
//     }
//   }
  .code-image{
    display: inline-block;
    vertical-align: top;
    width: 71px;
    height: 32px;
    // cursor: pointer;
    margin-left: 10px;
    position: absolute;
    right: 10px;
    top: 9px;
  }
}
</style>
